package com.jzy.message.activities;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.jcodecraeer.xrecyclerview.XRecyclerView;
import com.jzy.message.R;
import com.jzy.message.activities.adapter.addpopleAdapter;
import com.jzy.message.activities.base.AppConfig;
import com.jzy.message.activities.bean.ChengYuanBean;
import com.jzy.message.activities.bean.CodeBean;
import com.jzy.message.activities.bean.ReturnStatusResultEntity;
import com.jzy.message.activities.bean.addBean;
import com.jzy.message.activities.bean.addqunliaoBean;

import java.util.ArrayList;
import java.util.List;

public class messegeQunLiaoActivity extends AppCompatActivity{
    private  String room;
    private LinearLayoutManager linearLayoutManager;
    private addBean bean;
    private CodeBean codebean;
    private List<addBean.DataBean> list;
    private addpopleAdapter adapter;
    private List list_temp=new ArrayList();
    private ImageView iv_common_back;
    private TextView tv_common_title;
    private XRecyclerView xrecycler;
    private TextView tv_qunName;
    private addqunliaoBean addqunliaoBean;
    private String username;
    private TextView tv_hy;
    private String ids;
    private TextView tv_common_right;
    ChengYuanBean serInfo;
    public static LocalBroadcastManager localBroadcastManager;//注册广播
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.qun_liao);
        room=getIntent().getStringExtra("room");
        username=getIntent().getStringExtra("username");

         serInfo = (ChengYuanBean) getIntent().getSerializableExtra("serinfo");



        initView();
        localBroadcastManager= LocalBroadcastManager.getInstance(this);//注册广播
    }

    private void initView() {
        adapter=new addpopleAdapter(list,messegeQunLiaoActivity.this,serInfo);
        iv_common_back = (ImageView) findViewById(R.id.iv_common_back);
        iv_common_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        tv_common_title = (TextView) findViewById(R.id.tv_common_title);
        tv_common_title.setText("添加好友");
        tv_common_right = (TextView) findViewById(R.id.tv_common_right);
        tv_common_right.setText("添加");
        tv_common_right.setVisibility(View.VISIBLE);
        tv_common_right.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                ChuangJian();
            }
        });
        tv_qunName = (TextView) findViewById(R.id.tv_qunName);
        tv_qunName.setText(room);
        tv_hy = (TextView) findViewById(R.id.tv_hy);
        tv_hy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                getFriends();
            }
        });
        xrecycler = (XRecyclerView) findViewById(R.id.xrecycler);
        linearLayoutManager=new LinearLayoutManager(getApplicationContext());
        xrecycler.setLayoutManager(linearLayoutManager);
        xrecycler.setLoadingMoreEnabled(false);
        xrecycler.setPullRefreshEnabled(false);
    }
    private void getFriends() {
        String url= AppConfig.JASON_SERVICE_URL+"appFriends/getFriends?userName="+ username;
        AppConfig.getInst().getHttpRequestJsonClass(url, "1111", addBean.class, new AppConfig.Callback() {
            @Override
            public void onSuccess(Object o) {
                bean=(addBean)o;
                if (bean.getStatus() == 1){
                    if(bean.getData() != null && bean.getData().size()>0){
                        list=bean.getData();
                        adapter=new addpopleAdapter(list,messegeQunLiaoActivity.this,serInfo);
                        xrecycler.setAdapter(adapter);
                        adapter.notifyDataSetChanged();

                    }else {
                        list=new ArrayList<addBean.DataBean>();
                        adapter=new addpopleAdapter(list, messegeQunLiaoActivity.this, serInfo);
                        xrecycler.setAdapter(adapter);
                        adapter.notifyDataSetChanged();
                    }
                }
                else {
                    Toast.makeText(messegeQunLiaoActivity.this,bean.getMsg(),Toast.LENGTH_LONG).show();
                }
                xrecycler.refreshComplete();
            }

            @Override
            public void onFailure(ReturnStatusResultEntity error) {
                if(bean !=null){
                    if(bean.getData() != null && bean.getData().size()>0) {
                        bean.getData().clear();
                        Toast.makeText(messegeQunLiaoActivity.this, bean.getMsg(), Toast.LENGTH_LONG).show();
                        adapter.notifyDataSetChanged();
                        xrecycler.refreshComplete();
                    }
                }
            }
        });
    }
    private void ChuangJian(){
        list_temp= adapter.list_temp;
        if(list_temp.size()==0){
            Toast.makeText(messegeQunLiaoActivity.this, "请选择好友", Toast.LENGTH_SHORT).show();
            return;
        }
        if(list_temp.size()>0){
            for(int i=0;i<list_temp.size();i++){
                if(i==0){
                    ids=list_temp.get(i).toString()+",";
                }else {
                    ids=list_temp.get(0).toString()+",";
                    ids+=list_temp.get(i).toString()+",";
                }
            }
        }
        String ids1=ids.substring(0,ids.length()-1);
        addqunliaoBean=new addqunliaoBean();
        addqunliaoBean.setRoomName(room);
        addqunliaoBean.setDistinguish(2);
        addqunliaoBean.setIds(ids1);

        String url= AppConfig.JASON_SERVICE_URL+"qlxroomuserrelation/saveById";
        AppConfig.getInst().postHttpRequestJsonClass(url, "ssss",addqunliaoBean , CodeBean.class, new AppConfig.Callback() {
            @Override
            public void onSuccess(Object o) {
                codebean = (CodeBean) o;
                if (codebean.getStatus() == 1) {
                    //发送广播 刷新好友列表
                    Intent intent = new Intent("shuaxinhaoyouliebiao");
                    localBroadcastManager.sendBroadcast(intent);
                    Toast.makeText(messegeQunLiaoActivity.this, codebean.getMsg(), Toast.LENGTH_LONG).show();
                    finish();
                }
            }

            @Override
            public void onFailure(ReturnStatusResultEntity error) {
                Toast.makeText(messegeQunLiaoActivity.this, "添加失败", Toast.LENGTH_LONG).show();
            }
        });
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
    }

}
